﻿using Sustainalytics.ESG.Entities;
using System.Collections.Generic;
using System.Linq;
using EsgResearchProfile = Sustainalytics.SharedEntities.EsgResearch.EsgResearchProfile;

namespace Sustainalytics.ClientDW.Synchronization.SyncESGData.CompanyKeyESGIssueLogic
{
    public class CompanyKeyESGIssueMapper
    {
        public CompanyKeyESGIssue Map(EsgResearchProfile entity)
        {
            return new CompanyKeyESGIssue
            {
                Id = entity.Id,
                CompanyName = entity.CompanyName,
                SubPeerGroupId = entity.KeyIssues.Select(ki => ki.SubPeerGroupId).FirstOrDefault(),
                KeyESGIssues = entity.KeyIssues.Select(ki => new KeyEsgIssue
                {
                    Id = ki.Id,
                    Name = ki.Name.Value.Timestamped(ki.Name.Timestamp),
                    Description = ki.Description,
                    ExternalId = ki.ExternalId,
                    Comment = ki.Comment.Value.Timestamped(ki.Comment.Timestamp),
                    SortKey = ki.SortKey.Value.Timestamped(ki.SortKey.Timestamp),
                    KeyEsgIssueIndicators = ki.KeyEsgIssueIndicators?.Select(x => x.Map()).ToList(),
                    BussinessImpact = ki.BusinessImpact,
                    SustainabilityImpact = ki.SustainabilityImpact
                }).ToList()
            };
        }

        public IList<CompanyKeyESGIssue> MapManyToMany(IList<EsgResearchProfile> entities)
        {
            return entities.Select(Map).ToList();
        }
    }

    public static class KeyEsgIssueIndicatorExtensions
    {
        public static KeyEsgIssueIndicator Map(this SharedEntities.EsgResearch.KeyEsgIssueIndicator entity)
        {
            return new KeyEsgIssueIndicator
            {
                Type = (KeyEsgIssueIndicatorType)entity.Type,
                Number = entity.Number,
                Name = entity.Name,
                Score = entity.Score,
                Average = entity.Average,
                BestPractice = entity.BestPractice,
                MaterialBestPracticeGap = entity.MaterialBestPracticeGap,
                HidenToClient = entity.HidenToClient,
                AnswerCategory = entity.AnswerCategory,
                IsControversyIndicator = entity.IsControversyIndicator,
            };
        }
    }
}
